package com.example.tool.zop;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import static com.example.tool.zop.EncryptionType.MD5;


public class CallFun {


    /**
     *
     */
    private static String appKey = "22f4fdef307235c60118f";
    private static String appSecret = "6bc97e03c9c19da6ef90d4e6ceef1405";
    private static String wtqUrl = "https://japi.zto.com/zto.merchant.waybill.track.query";
    private static String bdeaUrl = "https://japi.zto.com/zto.open.bindingEaccount";

    //    /**
//     * 测试地址
//     */
//    private static String appKey="a46aa7302b717b9b52467";
//    private static String appSecret="c53451feced6985d2aae7c57722c0593";
//    private static String wtqUrl="https://japi-test.zto.com/zto.merchant.waybill.track.query";
//    private static String bdeaUrl="https://japi-test.zto.com/zto.open.bindingEaccount";
    public static void main(String[] args) throws IOException {
        List<String> list = Arrays.asList(
                "78507584135047"
        );
        Set<String> set = new LinkedHashSet<>(list);
        List<String> listWithoutDuplicates = new ArrayList<>(set);
        for (String no : listWithoutDuplicates) {

            System.out.println(wayBillTrackQuery(no));

        }
//        ZopClient client = new ZopClient("d093e54281d338145d609", "34fd16df9930c0ba87a42e1bace27ffc");
//        ZopPublicRequest request = new ZopPublicRequest();
//        request.setBody("{\"billCode\":\"78899106353455\",\"mobilePhone\":\"\"}");
//        request.setUrl("https://japi-test.zto.com/zto.merchant.waybill.track.query");
//        request.setBase64(true);
//        request.setEncryptionType(MD5);
//        request.setTimestamp(null);
//        System.out.println(client.execute(request));

    }

    /**
     * 2，4，-2 ，4，10，28，11
     * <p>
     * 绑定电子面单
     *
     * @param eaccount      电子面单账号
     * @param siteCode      网点code
     * @param a0ddress      地址：省市区
     * @param addressDetail 详细地址
     * @param eaccountPwd   电子面单密码
     */
    public static void bindingEAccount(String eaccount, String siteCode, String a0ddress, String addressDetail, String eaccountPwd) {
        ZopClient client = new ZopClient(appKey, appSecret);
        ZopPublicRequest request = new ZopPublicRequest();
        JSONObject body = new JSONObject();
        body.put("eaccount", eaccount);
        body.put("siteCode", siteCode);
        body.put("address", a0ddress);
        body.put("addressDetail", addressDetail);
        body.put("eaccountPwd", eaccountPwd);
        request.setBody(new Gson().toJson(body));
        request.setUrl(bdeaUrl);
        request.setBase64(true);
        request.setEncryptionType(MD5);
        request.setTimestamp(null);
        String execute = null;
        try {
            execute = client.execute(request);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        System.out.print(execute);
    }

    /**
     * @param billCode    运单号
     * @param mobilePhone 收寄人任一方电话号码后4位（手机或座机）。
     *                    通过电话号码鉴权时必填，鉴权方式可以电子面单账号或电话号码二选一。
     *                    选择电子面单账号鉴权时，该字段非必填；
     *                    选择电话号码鉴权时，可以不绑定下单电子面单账号。
     */
    public static JSONObject wayBillTrackQuery(String billCode, String mobilePhone) {
        ZopPublicRequest request = new ZopPublicRequest();
        request.setUrl(wtqUrl);
        JSONObject body = new JSONObject();
        body.put("billCode", billCode);
        body.put("mobilePhone", mobilePhone);
        request.setBody(new Gson().toJson(body));
        request.setBase64(true);
        request.setEncryptionType(MD5);
        request.setTimestamp(null);
        ZopClient client = new ZopClient(appKey, appSecret);
        String execute = null;
        try {
            execute = client.execute(request);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return JSONObject.parseObject(execute);
    }

    /**
     * 物流轨迹查询
     *
     * @param billCode 运单号
     */
    public static JSONObject wayBillTrackQuery(String billCode) {
        return wayBillTrackQuery(billCode, null);
    }

    /**
     * 物流轨迹查询
     *
     * @param billCodes 运单号 ，隔开
     */
    public static JSONArray wayBillTracksQuery(String billCodes) {
        JSONArray datas = new JSONArray();
        Arrays.stream(billCodes.split(",")).distinct().forEach(billCode -> {
            datas.add(wayBillTrackQuery(billCode));
        });
        return datas;
    }
}
